﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using OBDUDSUtils.CNNMode;
using OBDUDSUtils.Common;

namespace DeviceTestStand
{
    public partial class FrmAddPlatform : Form
    {
        private List<PlatformMessage> platformData;
        public PlatformMessage currentPlatform;
        public FrmAddPlatform(List<PlatformMessage> data)
        {
            InitializeComponent();
            this.platformData = data;
        }

        private void FrmAddPlatform_Load(object sender, EventArgs e)
        {
            var file = System.Environment.CurrentDirectory + "\\data\\";
            var fileinfos = new DirectoryInfo(file).GetFiles();
            foreach (var finfo in fileinfos)
            {
                this.cb_scripts.Items.Add(finfo.Name);
            }

            this.btn_cancel.Click += Btn_cancel_Click;
            this.btn_ok.Click += Btn_ok_Click;
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            var platform = this.tb_name.Text.Trim();
            if (string.IsNullOrEmpty(platform))
            {
                MessageBox.Show("平台名称不能为空!");
                return;
            }
            if (IsExistPlatform(platform))
            {
                MessageBox.Show($"{platform}已存在！请重新编辑！");
                return;
            }

            if (string.IsNullOrEmpty(this.cb_scripts.Text))
            {
                MessageBox.Show("请选择脚本！");
                return;
            }

            this.currentPlatform = new PlatformMessage()
            {
                Platforms = platform,
                ScriptName = this.cb_scripts.Text,
                SelectSafeVerify = this.ck_safeCheck.Checked + ""
            };

            this.platformData.Add(this.currentPlatform);

            this.Close();
            this.DialogResult = DialogResult.OK;
        }

        private void Btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool IsExistPlatform(string platform)
        {
            if (this.platformData.Find(p => p.Platforms == platform) == null)
                return false;
            return true;
        }
    }
}
